package com.uniview.plugins.adapter;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.uniview.entity.po.Employees;

/**
 * 员工操作
 * 
 * @author Leiqingtian
 *
 */
@Service("ad_EmployeesService")
public class Ad_EmployeesService {

	private Logger logger = Logger.getLogger(Ad_EmployeesService.class);

	@Autowired
	private MSDataSource mSDataSource;

	@Value("#{configProperties['ms.table_empolyees']}")
	private String employeesTableName;

	/**
	 * 获取员工
	 * 
	 * @return
	 */
	public Integer getTotalRows() {
		try {
			String sql = "select count(*) from " + employeesTableName;
			System.out.println(sql);
			return (Integer) mSDataSource.getCount(sql);
		} catch (Exception e) {
			logger.error("获取员工记录数异常", e);
		}
		return 0;
	}

	/**
	 * 分页查找所有的员工
	 * 
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public List<Employees> findEmployeesByPage(int pageSize, int pageNo) {
		try {
			String sql = "select * from (select row_number() over(order by tempcolumn) temprownumber,* "
					+ " from (select top " + pageSize * pageNo + " tempcolumn = 0,* from " + employeesTableName
					+ ") t )tt" + " where temprownumber > " + pageSize * (pageNo - 1);
			System.out.println(sql);
			return mSDataSource.queryForList(Employees.class, sql);
		} catch (Exception e) {
			logger.error("获取员工记录异常", e);
		}
		return null;
	}

	/**
	 * 查找所有的员工
	 * 
	 * @return
	 */
	public List<Employees> findAllEmployees() {
		try {
			String sql = "select * from " + employeesTableName;
			return mSDataSource.queryForList(Employees.class, sql);
		} catch (Exception e) {
			logger.error("获取员工记录异常", e);
		}
		return null;
	}

	/**
	 * 获取时间端内的员工信息
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public List<Employees> findEmployeesByTime(String beginDate, String endDate) {
		try {
			String sql = "select * from " + employeesTableName + " where (LastUpdatedDate between '" + beginDate
					+ "' and '" + endDate + "') or (CreatedDate between '" + beginDate + "' and '" + endDate + "')";
			logger.info(sql);
			return mSDataSource.queryForList(Employees.class, sql);
		} catch (Exception e) {
			logger.error("获取时间段内的人员信息异常", e);
		}
		return null;
	}
}
